using System.Collections.Generic;
using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class BadgeMenu : Script
{
    public static UIMenu menu = new UIMenu("", TextSetting.bg_Menu, new Point(0, -107));
    private static bool inited;
    private static List<dynamic> badgeList = new List<object>(new string[4] { "1", "2", "3", "4" });
    private static UIMenuListItem badgeItem;
    private static int gid;

    public BadgeMenu()
    {
        if (!inited)
        {
            Main.pool.Add(menu);
            menu.OnListChange += OnListChange;
            menu.OnMenuClose += OnMenuClose;
            menu.OnItemSelect += OnItemSelect;
            inited = true;
        }
    }

    public static void InitBadgeMenu()
    {
        menu.Clear();
        menu.SetBannerType(new UIResRectangle());
        badgeItem = new UIMenuListItem(TextSetting.tb_ListItem_Group, badgeList, gid);
        menu.AddItem(badgeItem);
        menu.AddItem(new UIMenuItem($"{TextSetting.tb_MenuItem_Torso} {badgeList[badgeItem.Index]}", TextTipsChange(0, gid)));
        menu.AddItem(new UIMenuItem($"{TextSetting.tb_MenuItem_Head} {badgeList[badgeItem.Index]}", TextTipsChange(1, gid)));
        menu.AddItem(new UIMenuItem($"{TextSetting.tb_MenuItem_LeftArm} {badgeList[badgeItem.Index]}", TextTipsChange(2, gid)));
        menu.AddItem(new UIMenuItem($"{TextSetting.tb_MenuItem_RightArm} {badgeList[badgeItem.Index]}", TextTipsChange(3, gid)));
        menu.AddItem(new UIMenuItem($"{TextSetting.tb_MenuItem_LeftLeg} {badgeList[badgeItem.Index]}", TextTipsChange(4, gid)));
        menu.AddItem(new UIMenuItem($"{TextSetting.tb_MenuItem_RightLeg} {badgeList[badgeItem.Index]}", TextTipsChange(5, gid)));
        menu.AddItem(new UIMenuItem($"{TextSetting.tb_MenuItem_ClearGroup} {badgeList[badgeItem.Index]}"));
        for (int i = 1; i < 7; i++)
        {
            menu.BindMenuToItem(BadgeSubMenu.menu, menu.MenuItems[i]);
        }
        menu.RefreshIndex();
        CheckCanUseMenuItem();
        menu.Visible = true;
    }

    public static void CheckCanUseMenuItem()
    {
        int[] counts = new int[6];
        if (DressingScript.Dressing == Main.Member.P2)
        {
            if (Main.P2.skin.BadgeData == null) Main.P2.skin.GetDecorationData(Decoration.Type.TYPE_BADGE);
            for (int i = 0; i < Main.P2.skin.BadgeData.Length; i++)
            {
                for (int j = 0; j < Main.P2.skin.BadgeData[i].Count; j++)
                {
                    if (Main.P2.skin.BadgeData[i][j].nameHash != string.Empty) counts[i]++;
                }
            }
        }
        else
        {
            if (Main.P1.skin.BadgeData == null) Main.P1.skin.GetDecorationData(Decoration.Type.TYPE_BADGE);
            for (int i = 0; i < Main.P1.skin.BadgeData.Length; i++)
            {
                for (int j = 0; j < Main.P1.skin.BadgeData[i].Count; j++)
                {
                    if (Main.P1.skin.BadgeData[i][j].nameHash != string.Empty) counts[i]++;
                }
            }
        }
        for (int m = 1; m < 7; m++) menu.MenuItems[m].Enabled = false;
        for (int n = 1; n < 7; n++) if (counts[n - 1] != 0) menu.MenuItems[n].Enabled = true;
    }

    private static void OnListChange(UIMenu sender, UIMenuListItem list, int index)
    {
        gid = badgeItem.Index;
        InitBadgeMenu();
    }

    private static string TextTipsChange(int zid, int gid)
    {
        string result = TextSetting.tb_TextTips_NULL;
        if (DressingScript.Dressing == Main.Member.P2)
        {
            if (Main.P2.skin.Badge[zid][gid].action) result = TextSetting.tb_TextTips_InUse;
        }
        else if (Main.P1.skin.Badge[zid][gid].action)
        {
            result = TextSetting.tb_TextTips_InUse;
        }
        return result;
    }

    private static void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
    {
        if (index >= 1 && index < 7)
        {
            BadgeSubMenu.InitBadgeSubMenu(index - 1, gid);
        }
        if (index == 7)
        {
            if (DressingScript.Dressing == Main.Member.P2)
            {
                Main.P2.skin.ClearDecoration(Decoration.Type.TYPE_BADGE, gid);
            }
            else
            {
                Main.Clone.skin.ClearDecoration(Decoration.Type.TYPE_BADGE, gid);
                Main.P1.skin.ClearDecoration(Decoration.Type.TYPE_BADGE, gid);
            }
            InitBadgeMenu();
        }
    }

    private static void OnMenuClose(UIMenu sender)
    {
        menu.Visible = false;
        // 不刷新
    }
}


